package com.xunsiya.controller.media;

import com.xunsiya.common.config.RequestUtils;
import com.xunsiya.module.media.vo.Media;
import com.xunsiya.service.module.media.MediaService;
import com.xunsiya.tools.common.msg.Msg;
import com.xunsiya.tools.common.pagination.ForePage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 图像管理
 *
 * @version 1.0
 * @author: xml
 * @date: 2018/10/31
 */
@RestController
@RequestMapping("/api/media")
public class MediaController {

    @Autowired
    private MediaService mediaService;

    /**
     * 指纹入库
     *
     * @param media 图像
     * @return
     */
    @PostMapping("/save")
    public Msg save(@RequestBody Media media) {
        String token = media.getToken();
        List<Map<String, String>> filePaths = RequestUtils.getSessionAttribte(token);
        List<Map<String, String>> extFilePaths = RequestUtils.getSessionAttribte(media.getExtToken());
        if (!CollectionUtils.isEmpty(filePaths)) {
            String extFilePath = null;
            if (!CollectionUtils.isEmpty(extFilePaths)) {
                extFilePath = extFilePaths.get(0).get("filePath");
            }
            Integer companyId = media.getCompanyId();
            if (companyId == null || companyId <= 0) {
                return new Msg(false, "没有公司信息，保存失败!");
            } else {
                for (Map<String, String> fileMap : filePaths) {
                    String filePath = fileMap.get("filePath");
                    media.setMediaPath(filePath);
                    media.setExtraInfo(extFilePath);
                    mediaService.save(media);
                }
                return new Msg(true, "添加成功！");
            }
        }
        return new Msg(false, "文件上传失败!");
    }

    /**
     * 分页查询指纹
     *
     * @param media 图像
     * @return
     */
    @RequestMapping("/query")
    public ForePage<Media> query(@RequestBody Media media) {

        int pageIndex = RequestUtils.getIntParam("pageIndex") == null ? 1 : RequestUtils.getIntParam("pageIndex");
        int pageSize = RequestUtils.getIntParam("pageSize") == null ? 100 : RequestUtils.getIntParam("pageSize");
        return mediaService.querySiftForPage(media, pageIndex, pageSize);
    }


    /**
     * 删除指纹
     *
     * @param media 图像
     * @return
     */
    @PostMapping("/deleteMedia")
    public Msg deleteMedia(@RequestBody Media media) {
        Integer id = media.getId();
        Integer companyId = media.getCompanyId();
        return mediaService.deleteSift(id, companyId);
    }

    /**
     * 修改指纹
     *
     * @param media 图像
     * @return
     */
    @PostMapping("/updateMedia")
    public Msg updateMedia(@RequestBody Media media) {
        Integer companyId = media.getCompanyId();

        // 去的上传的附件
        List<Map<String, String>> extFilePaths = RequestUtils.getSessionAttribte(media.getExtToken());
        if (!CollectionUtils.isEmpty(extFilePaths)) {
            media.setExtraInfo(extFilePaths.get(0).get("filePath"));
        }

        if (companyId == null || companyId <= 0) {
            return new Msg(false, "没有公司信息，修改失败!");
        } else {
            mediaService.update(media);
            return new Msg(true, "图像修改成功！");
        }
    }

}
